package org.getOffer.offer48;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author yu
 * @Date 2021/10/23
 */
public class Solution {

    public static void main(String[] args) {

        //String str = " ";

        //String str = "abcabcbb";

        //String str = "bbbbb";

        //String str = "tmmzuxt";

        String str = "pwwkew";

        Solution solution = new Solution();

        System.out.println(solution.lengthOfLongestSubstring(str));

    }

    public int lengthOfLongestSubstring(String s) {

//        if (s.length() == 0) {
//            return 0;
//        }
//
//        int[] arr = new int[26];
//
//        for (int i = 0; i < arr.length; i++) {
//            arr[i] = -1;
//        }
//
//        char[] chars = s.toCharArray();
//
//        int right = 0, max = 0;
//
//        for (int left = 0; left < s.length(); left++) {
//            int index = chars[left] - 97;
//            if (arr[index] != -1 ) {
//                right = arr[index] + 1;
//                arr[index] = left;
//                continue;
//            }
//            max = Math.max(max, left - right);
//            arr[index] = left;
//        }
//
//        return max+1;

        if (s.length() == 0) {
            return 0;
        }

        Map<Character, Integer> map = new HashMap<>();

        char[] chars = s.toCharArray();

        int right = 0, max = 0;

        for (int left = 0; left < s.length(); left++) {
            if (map.get(chars[left]) != null && map.get(chars[left]) >= right) {
                right = map.get(chars[left]) + 1;
                map.put(chars[left], left);
                continue;
            }
            max = Math.max(max, left - right);
            map.put(chars[left], left);
        }

        return max+1;

    }

}
